package com.yinhe.music.yhmusic.personal.adapter;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.pay.PayActivity;
import com.yinhe.music.yhmusic.personal.model.VipListInfo;

import java.util.List;
import java.util.Locale;

/**
 * Created by hasee on 2017/5/5.
 */

public class VipListAdapter extends RecyclerView.Adapter<VipListAdapter.ItemView> {

    private List<VipListInfo> mList;
    private Activity activity;

    public VipListAdapter(List<VipListInfo> list, Activity activity) {
        mList = list;
        this.activity = activity;
    }

    public void update(List<VipListInfo> list) {
        mList = list;
        notifyDataSetChanged();
    }

    @Override
    public ItemView onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new ItemView(layoutInflater.inflate(R.layout.vip_list_item, parent, false));
    }


    @Override
    public void onBindViewHolder(final ItemView holder, int position) {
        if (mList != null) {
            final VipListInfo info = mList.get(position);
            holder.number.setText(String.format(Locale.CHINESE, "%d天", info.getDays()));
            holder.price.setText(String.format(Locale.CHINESE, "%d元", info.getPrice()));
            holder.open.setOnClickListener(view -> {
                Intent intent = new Intent(activity, PayActivity.class);
                intent.putExtra("price", String.valueOf(info.getPrice()));
                intent.putExtra("productstype", IConstants.PRODUCT_MEMBER);
                intent.putExtra("products", String.valueOf(info.getDays()));
                activity.startActivity(intent);
            });
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        } else {
            return 3;
        }

    }


    class ItemView extends RecyclerView.ViewHolder {
        private TextView number, price;
        private Button open;

        public ItemView(View itemView) {
            super(itemView);
            number = itemView.findViewById(R.id.number);
            price  = itemView.findViewById(R.id.price);
            open   = itemView.findViewById(R.id.open);
        }
    }

}
